package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 地址展示
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(HttpSession session){
        // 获取用户id
        Object user = session.getAttribute("user");
        log.info("登录用户:{}",user);
        // 根据用户id去查询地址数据
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,user);
        List<AddressBook> list = addressBookService.list(queryWrapper);
        // 将查询的数据集合返回
        return R.success(list);
    }

    /**
     * 新建收货地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> selectAddressBook(@RequestBody AddressBook addressBook,HttpSession session){
        log.info("提交地址数据:{}",addressBook);
        // 获取用户id
        Long user = (Long) session.getAttribute("user");
        // 添加用户id
        addressBook.setUserId(user);

        // 保存接收数据
        addressBookService.save(addressBook);

        return R.success("地址创建成功!");
    }

    /**
     * 收货地址修改信息显示
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> getAddressBook(@PathVariable Long id){
        log.info("地址修改显示:{}",id);
        // 根据前端返回的id,查询该条地址信息
        AddressBook addressBook = addressBookService.getById(id);

        // 将查询数据返回
        return R.success(addressBook);
    }

    /**
     * 收货地址修改
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> putAddressBook(@RequestBody AddressBook addressBook){
        log.info("提交修改数据:{}",addressBook);
        // 修改
        boolean update = addressBookService.updateById(addressBook);
        if (update){
            return R.success("修改成功!");
        }
        return R.error("修改失败!");
    }

    /**
     * 地址删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteAddressBook(Long ids){
        log.info("删除地址id:{}",ids);
        // 删除地址
        addressBookService.removeById(ids);

        return R.success("删除成功!");
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @param session
     * @return
     */
    @PutMapping("/default")
    public R<AddressBook> defaultAddressBook(@RequestBody AddressBook addressBook,HttpSession session){
        log.info("默认地址id:{}",addressBook.getId());
        Long id = addressBook.getId();
        // 把该用户全部的地址的default修改为0,然后重新设置default(默认地址)
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,session.getAttribute("user"));
        List<AddressBook> list = addressBookService.list(queryWrapper);
        for (AddressBook book : list) {
            book.setIsDefault(0);
            // 把地址的default修改为0(0 :非默认地址  1:默认地址)
            addressBookService.updateById(book);
        }

        // 把指定地址设置为默认地址
        AddressBook byId = addressBookService.getById(id);
        byId.setIsDefault(1);
        addressBookService.updateById(byId);

        // 返回修改后的结果
        return R.success(byId);
    }

    /**
     * 订单配送地址
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> getDefault(){
        // 获取用户id
        Long userId = BaseContext.getCurrentId();
        // 根据用户id和默认地址查询默认地址
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,userId).eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(queryWrapper);

        return R.success(addressBook);
    }
}
